﻿<cfscript>
/** 
* ORM Building
* 
* @hint "Hibernate persistenter t_building. 建筑物的基本数据项" 
* 
*/ 

component 
	displayname="public.service.orm.senate.Building" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="Building" 
	table="t_building"
	{

	property type="string" name="BuildingID" column="b_id" fieldtype="id" generator="assigned" hint="建筑物号";
	
	property type="string" name="BuildingName" column="b_name" hint="建筑物名称";
	property type="string" name="Floors" column="b_floors" hint="建筑物层数";

	property name="Campus" fieldtype="many-to-one" fkcolumn="campus_id" cfc="Campus" hint="校区代码";

	property name="Classrooms" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="b_id" cfc="Classroom" hint="教学楼中教室";

	/**
	* @hint "自定义主键规则"
	* 
	* @CampusID "校区代码"
	*/
	public string function getNextID( required string CampusID ) output=false {
		
		var nextID = arguments.CampusID & numberFormat(1, "0___");
		var checkArray = ORMExecuteQuery("select BuildingID from Building 
											where BuildingID like :condition 
											order by BuildingID desc", {condition = arguments.CampusID & "____"}, { maxResults=1 });

		/* 
			主键生成规则 CHAR(6)
			
			2位校区代码 + 4位为流水号
			
		*/
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = arguments.CampusID & numberFormat((right(checkArray[1], 4) + 1), "0___");
			
		}

		return nextID;

	}
}

</cfscript>